//
//  GKVisitorView.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/8.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit

//定义协议
protocol GKVisitorViewDelegate:NSObjectProtocol
{
    func visitorViewDidClickBtnRegister()
    func visitorViewDidClickBtnLogin()
}



class GKVisitorView: UIView {

    //定义代理属性
    var delegate: GKVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //外部调用赋值方法
    func setVisitorViewInfo( message:String,imageName: String)
    {
        self.lblMessage.text = message
        self.iconView.image = UIImage(named: imageName)
        self.homeView.hidden = true
        self.coverView.hidden = true
    }
    
    //外部调用旋转方法
    func startRotation()
    {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.removedOnCompletion = false
        self.iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    private func prepareUI()
    {
        // 1.添加控件

        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(homeView)
        self.addSubview(lblMessage)
        self.addSubview(btnLogin)
        self.addSubview(btnRegist)
        
        // 禁用Autoresizing
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        lblMessage.translatesAutoresizingMaskIntoConstraints = false
        btnRegist.translatesAutoresizingMaskIntoConstraints = false
        btnLogin.translatesAutoresizingMaskIntoConstraints = false
        
        // 2.添加约束
        // 约束都往父控件添加
        /// item: 要约束的view
        /// attribute: 要约束的view的什么属性
        /// relatedBy: 参照关系
        /// toItem: 要参照的view
        /// attribute: 参照的view的什么属性
        
        // 将约束添加到父控件身上
        /// 转轮
        self.addConstraint(NSLayoutConstraint(item: self.iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: self.iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        /// 遮盖
        addConstraint(NSLayoutConstraint(item: self.coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: self.coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: self.coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: self.coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        
        /// 房子
        addConstraint(NSLayoutConstraint(item: self.homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: self.homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        
        
        /// 消息label
        /// 当参照的view为nil时,attribute必须设置为NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: self.lblMessage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: self.lblMessage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: self.lblMessage, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        
        
        
        /// 注册按钮        /// 阿拉伯语言
        addConstraint(NSLayoutConstraint(item: btnRegist, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.lblMessage, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: btnRegist, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: lblMessage, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: btnRegist, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: btnRegist, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
        /// 登录按钮
        addConstraint(NSLayoutConstraint(item: btnLogin, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: lblMessage, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: btnLogin, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: lblMessage, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: btnLogin, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
    
        addConstraint(NSLayoutConstraint(item: btnLogin, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
    }
    
    //点击注册,登录按钮
    @objc private func clickRegisterIssue() {
        self.delegate?.visitorViewDidClickBtnRegister()
    }
    @objc private func clickLoginIssue() {
        self.delegate?.visitorViewDidClickBtnLogin()
    }
    
    
    //  懒加载控件
    //注册
    private lazy var btnRegist:UIButton = {
       
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(self.clickRegisterIssue), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    //登录
    private lazy var btnLogin: UIButton = {
       
        let btn = UIButton()
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(self.clickLoginIssue), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    //信息
    private lazy var lblMessage: UILabel = {
        let lbl = UILabel()
        
        lbl.text = "关注一些人!看看有神马惊喜!"
        lbl.font = UIFont.systemFontOfSize(18)
        lbl.numberOfLines = 0
        lbl.textAlignment = NSTextAlignment.Center
        return lbl
    }()
    /// 转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 遮盖
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
}
